using System;
using System.Collections.Generic;
using System.Linq;
using RFS.BizLogic.PaiShi.Ports;
using RFS.Domain.PaiShi;
using RFS.Domain.Sys;

namespace RFS.BizLogic.PaiShi.Impl
{
    public class PaiShiOrgOrg : IPaiShiOrg
    {
        private List<IHolidayPlan> _holidayPlans=new List<IHolidayPlan>();
        private IConfigT _config;

        public PaiShiOrgOrg(IPriceManagerPort priceManager, IConfigT config)
        {
            PriceManager = priceManager;
            _config = config;
        }

        public decimal GetPrice(DateTime date,string supplier, string ProdName, int length)
        {
            if (IsItInHoliday(date))
            {
                throw new Exception($"[{date:d}]是假期没有交易价。");
                
            }
            
           return PriceManager.GetPrice(date, supplier, ProdName, length,_config.ConfigSys.SmartChoosePrice ).Price ;
        }

        public IEnumerable<IProductPriceItem> GetPriceItemsByDate(DateTime today)
        {
            return PriceManager.GetPriceItems(today);
        }

        public IEnumerable<IHolidayPlan> GetHolidayPlans()
        {

            var ls=new List<IHolidayPlan>();
            // for (int i = 0; i < 5; i++)
            // {
            //     ls.Add(new HolidayPlan()
            //     {
            //         StartAt = to
            //     });
            // }

            
            ls.Add(new HolidayPlan()
            {
                StartAt = DateTime.Parse("2025-1-1"),
                EndAt = DateTime.Parse("2025-1-1"),
                Remark = "元旦"
            });
            
                        
            ls.Add(new HolidayPlan()
            {
                StartAt = DateTime.Parse("2025-1-28"),
                EndAt = DateTime.Parse("2025-2-7"),
                Remark = "25春节"
            });
            
            
            return ls;
        }

        public IPriceManagerPort PriceManager { get; set; }

        private bool IsItInHoliday(DateTime date)
        {
            return (HolidayPlans.Any(x => date > x.StartAt && date < x.EndAt));


        }

        public IEnumerable<IHolidayPlan> HolidayPlans
        {
            get => _holidayPlans;
            set => _holidayPlans = value.ToList();
        }
    }
}